package com.tzkyzj.tiremanagersystem.viewmodel;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tzkyzj.tiremanagersystem.R;
import com.tzkyzj.tiremanagersystem.eneities.Tire;

import java.util.List;

public class TireListAdapter extends RecyclerView.Adapter<TireListAdapter.TireViewHolder> {

    private List<Tire> tireList;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Tire tire);
        void onDetailClick(Tire tire);
        void onRepairClick(Tire tire);
        void onEditClick(Tire tire);
    }

    public TireListAdapter(List<Tire> tireList, OnItemClickListener listener) {
        this.tireList = tireList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public TireViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_tire_list, parent, false);
        return new TireViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TireViewHolder holder, int position) {
        Tire tire = tireList.get(position);
        holder.bind(tire, listener);
    }

    @Override
    public int getItemCount() {
        return tireList != null ? tireList.size() : 0;
    }

    public void updateData(List<Tire> newTireList) {
        this.tireList = newTireList;
        notifyDataSetChanged();
    }

    static class TireViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTireNumber;
        private TextView tvPlateNumber;
        private TextView tvPosition;
        private TextView tvCompany;
        private TextView tvBrandModel;
        private TextView tvInstallDate;
        private TextView tvMileage;
        private TextView tvStatus;

        public TireViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTireNumber = itemView.findViewById(R.id.tv_tire_number);
            tvPlateNumber = itemView.findViewById(R.id.tv_plate_number);
            tvPosition = itemView.findViewById(R.id.tv_position);
            tvCompany = itemView.findViewById(R.id.tv_company);
            tvBrandModel = itemView.findViewById(R.id.tv_brand_model);
            tvInstallDate = itemView.findViewById(R.id.tv_install_date);
            tvMileage = itemView.findViewById(R.id.tv_mileage);
            tvStatus = itemView.findViewById(R.id.tv_status);
        }

        public void bind(Tire tire, OnItemClickListener listener) {
            // 设置轮胎号
            tvTireNumber.setText(tire.getTireNumber() != null ? tire.getTireNumber() : "--");

            // 设置车牌号
            tvPlateNumber.setText(tire.getDisplayPlateNumber());

            // 设置位置
            tvPosition.setText(tire.getFullPosition());

            // 设置运输公司
            tvCompany.setText(tire.getTransportCompany() != null ? tire.getTransportCompany() : "--");

            // 设置品牌型号
            String brandModel = "";
            if (tire.getTireBrand() != null && tire.getTireModel() != null) {
                brandModel = tire.getTireBrand() + " " + tire.getTireModel();
            } else if (tire.getTireBrand() != null) {
                brandModel = tire.getTireBrand();
            } else if (tire.getTireModel() != null) {
                brandModel = tire.getTireModel();
            } else {
                brandModel = "--";
            }
            tvBrandModel.setText(brandModel);

            // 设置安装日期
            tvInstallDate.setText(tire.getInstallDate() != null ? tire.getInstallDate() : "--");

            // 设置里程
            String mileageText = "--";
            if (tire.getCurrentMileage() != null) {
                mileageText = tire.getCurrentMileage() + "公里";
            } else if (tire.getInstallMileage() != null) {
                mileageText = tire.getInstallMileage() + "公里(初始)";
            }
            tvMileage.setText(mileageText);

            // 设置状态
            if (tire.getStatus() != null) {
                tvStatus.setText(tire.getStatus());
                if ("已报废".equals(tire.getStatus())) {
                    tvStatus.setBackgroundResource(R.drawable.bg_status_scrapped);
                    itemView.setAlpha(0.7f);
                } else {
                    tvStatus.setBackgroundResource(R.drawable.bg_status_active);
                    itemView.setAlpha(1.0f);
                }
            } else {
                tvStatus.setText("未知");
                tvStatus.setBackgroundResource(R.drawable.bg_status_active);
            }

            // 点击事件
            itemView.setOnClickListener(v -> listener.onItemClick(tire));

            // 详情按钮点击
            itemView.findViewById(R.id.btn_detail).setOnClickListener(v ->
                    listener.onDetailClick(tire));

            // 修改按钮点击
            itemView.findViewById(R.id.btn_edit).setOnClickListener(v ->
                    listener.onEditClick(tire));

            // 修补按钮点击（仅使用中的轮胎显示）
            //View btnRepair = itemView.findViewById(R.id.btn_repair);
            //if (tire.getScrapDate() == null) {
            //    btnRepair.setVisibility(View.VISIBLE);
            //    btnRepair.setOnClickListener(v -> listener.onRepairClick(tire));
            //} else {
            //    btnRepair.setVisibility(View.GONE);
           // }
        }
    }
}